﻿/**
 * ...
 * @author 
 */

package loader;
import client.ReceivedItem;

class ReceivedItemXmlParser 
{

	private var xml : Xml;
	
	public function new(xml : Xml) 
	{
		this.xml = xml;
	}
	
	public function parse() : Array<ReceivedItem>
	{
		var itemsArray = new Array<ReceivedItem>();
				
		var responseIterator = xml.iterator();
		if(responseIterator != null ) responseIterator.next();
				
		var itemsIterator = responseIterator.next().elementsNamed("item");
				
		while (itemsIterator.hasNext()) 
		{
			var itemXml = itemsIterator.next();
			
			var id 		: UInt	 = 0;
			var name 	: String = "";
			var link 	: String = "";
			var link_img : String = "";
			var desc	: String = "";
			var date 	: String = "";
			var author	: String = "";
			var chanId  : UInt = 0;
			var sender : String = "";
			var message_id : UInt = 0;
			var isRed : String = "";
			
			var attrIterator = itemXml.elements();
			
			while (attrIterator.hasNext()) 
			{
				var attribute = attrIterator.next();
				var attrName = attribute.nodeName;
				var attrValue = attribute.firstChild().nodeValue;
				
				switch (attrName) 
				{
					case "id":			id = Std.parseInt(attrValue);
					case "name":		name = attrValue;
					case "link":		link = attrValue;
					case "link_img":	link_img = attrValue;
					case "description":	desc = attrValue;
					case "date":		date = attrValue;
					case "author":		author = attrValue;
					case "id_ch":		chanId = Std.parseInt(attrValue);
					case "sender":	sender = attrValue;
					case "message": message_id = Std.parseInt(attrValue);
					case "isRed" : isRed = attrValue;
				}				
			}
			
			var item = new ReceivedItem(
									id,
									name,
									link,
									link_img,
									chanId,
									desc,
									date,
									author,
									sender, message_id, isRed
								);
										
			itemsArray.push(item);
		}		
		
		return itemsArray;
	}
	
}